import axios from "axios";
import { message } from "antd";
import { getToken } from "./auth";

// const baseURL = "http://47.100.120.129:9095/jingjian-api-v3.1"; //线上
// const baseURL = 'http://106.15.176.20:8012/guanjia-test' //测试
const baseURL = 'http://47.100.120.129:9095/jingjian-api-v3.1'//线上


const service = axios.create({
  baseURL,
  timeout: 5000,
});

// 配置请求拦截
// request拦截器
service.interceptors.request.use(
  function (config) {
    if (getToken()) {
      config.headers["Authorization"] = getToken();
    config.headers['type'] = 2;
    }
    // console.log(config)
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);
/*
@param
*/
// respone拦截器
service.interceptors.response.use(
  (response) => {
    /**
     * code为非200是抛错 可结合自己业务进行修改
     */
    const res = response.data;
    if (res.code !== "0") {
      message.info(res.message);
      //   if (res.code === 20003 || res.code === 20004) {
      //     MessageBox.alert("登录状态失效，请重新登录", "重新登录", {
      //       confirmButtonText: "重新登录",
      //       showClose: false,
      //       type: "warning",
      //       closeOnClickModal: false,
      //     }).then(() => {
      //       location.href = "/#/";
      //     });
      //   }
      return res;
      //   return Promise.reject(new Error(res.message || "Error"));
    } else {
      return response;
    }
  },
  (error) => {
    console.log("失败");
    console.log("err" + error); // for debug
    return Promise.reject(error);
  }
);
export default service;
